package com.qumitech.visualnovel.service.reviewdashboard.impl;

import com.qumitech.visualnovel.datasource.nocore.ChatMsgDOMapper;
import com.qumitech.visualnovel.datasource.app.*;
import com.qumitech.visualnovel.datasource.console.OKRConfDOMapper;
import com.qumitech.visualnovel.datasource.console.OKRDataDOMapper;
import com.qumitech.visualnovel.datasource.console.UserActDashboardDOMapper;
import com.qumitech.visualnovel.datasource.console.UserRegDashboardDOMapper;
import com.qumitech.visualnovel.model.dao.app.UserVoiceDriftDO;
import com.qumitech.visualnovel.model.dao.console.OKRConfDO;
import com.qumitech.visualnovel.model.dao.console.OKRDataDO;
import com.qumitech.visualnovel.model.dao.console.UserActDashboardDO;
import com.qumitech.visualnovel.model.dao.console.UserRegDashboardDO;
import com.qumitech.visualnovel.datasource.app.VoiceRoomMapper;
import com.qumitech.visualnovel.pubdef.dto.voiceroom.query.VoiceRoomDataQueryDTO;
import com.qumitech.visualnovel.pubdef.dto.review_dashboard.*;
import com.qumitech.visualnovel.service.reviewdashboard.ReviewDashBoardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.qumitech.visualnovel.model.common.BaseRsp;
import com.qumitech.visualnovel.model.common.ResultEnum;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSONArray;
import com.qumitech.visualnovel.component.util.DateUtil;

@Service
public class ReviewDashBoardServiceImpl implements ReviewDashBoardService {

    @Autowired
    UserContentRecordDTOMapper userContentRecordDTOMapper;
    @Autowired
    UserTrendDOMapper userTrendDOMapper;
    @Autowired
    TrendCommentDOMapper trendCommentDOMapper;
    @Autowired
    UserVoiceDriftDOMapper userVoiceDriftDOMapper;
    @Autowired
    ChatMsgDOMapper chatMsgDOMapper;
    @Autowired
    UserSkillAdminDOMapper userSkillAdminDOMapper;
    @Autowired
    AccompanyAppraiseDOMapper accompanyAppraiseDOMapper;
    @Autowired
    AccompanyAfterSaleApplyDOMapper accompanyAfterSaleApplyDOMapper;
    @Autowired
    UserTipDOMapper userTipDOMapper;
    @Autowired
    WithdrawApplyDOMapper withdrawApplyDOMapper;
    @Autowired
    OKRConfDOMapper okrConfDOMapper;
    @Autowired
    OKRDataDOMapper okrDataDOMapper;
    @Autowired
    VoiceRoomMapper voiceRoomMapper;
    @Autowired
    UserActDashboardDOMapper userActDashboardDOMapper;
    @Autowired
    UserRegDashboardDOMapper userRegDashboardDOMapper;

    private void getReviewUserInfoStatistic(String startTime, String endTime,  ReviewDashBoardDTO reviewDashBoardDTO)
    {
        ReviewUserInfoDTO reviewUserInfoDTO = new ReviewUserInfoDTO();
        ReviewVoiceRoomDTO reviewVoiceRoomDTO = new ReviewVoiceRoomDTO();
        List<HashMap<String, Long>> reviewMap = userContentRecordDTOMapper.getReviewUserInfoCount(startTime,endTime);
        System.out.println(reviewMap);
        for(HashMap<String, Long> map: reviewMap)
        {
            switch (Integer.parseInt(String.valueOf(map.get("content_type")))) {
                case 1:
                    reviewUserInfoDTO.setPhoto(map.get("data"));
                    break;
                case 3:
                    reviewUserInfoDTO.setAvatar(map.get("data"));
                    break;
                case 4:
                    reviewUserInfoDTO.setSign(map.get("data"));
                    break;
                case 6:
                    reviewVoiceRoomDTO.setBackgroud(map.get("data"));
                    break;
                case 9:
                    reviewUserInfoDTO.setNickname(map.get("data"));
                    break;
                case 12:
                    reviewUserInfoDTO.setVideo(map.get("data"));
                    break;
                case 13:
                    reviewVoiceRoomDTO.setFrontCover(map.get("data"));
                    break;
                case 14:
                    reviewVoiceRoomDTO.setName(map.get("data"));
                    break;
                case 15:
                    reviewVoiceRoomDTO.setNotice(map.get("data"));
                    break;
                case 16:
                    reviewUserInfoDTO.setVoice(map.get("data"));
                    break;
                case 17:
                    reviewUserInfoDTO.setAutoReply(map.get("data"));
                    break;
                case 18:
                    reviewUserInfoDTO.setWelcomeMsg(map.get("data"));
                    break;
                case 19:
                    reviewVoiceRoomDTO.setFriendSlogan(map.get("data"));
                    break;
            }
        }
        reviewDashBoardDTO.setReviewUserInfoDTO(reviewUserInfoDTO);
        reviewDashBoardDTO.setReviewVoiceRoomDTO(reviewVoiceRoomDTO);
    }

    private void getReviewSquareStatistic(String startTime, String endTime, ReviewDashBoardDTO reviewDashBoardDTO)
    {
        ReviewSquareDTO squareDTO = new ReviewSquareDTO();
        squareDTO.setTrend(userTrendDOMapper.getReviewUserTrendCount(startTime, endTime));
        squareDTO.setComment(trendCommentDOMapper.getReviewTrendCommentCount(startTime, endTime));
        squareDTO.setSoundBottle(userVoiceDriftDOMapper.getReviewUserVoiceDriftCount(startTime, endTime));
        reviewDashBoardDTO.setReviewSquareDTO(squareDTO);
    }

    private void getReviewChatStatistic(String startTime, String endTime, ReviewDashBoardDTO reviewDashBoardDTO)
    {
        ReviewChatDTO chatDTO = new ReviewChatDTO();
        chatDTO.setIm(Long.valueOf(99999));
        chatDTO.setVoiceroom(Long.valueOf(99999));
        chatDTO.setGroup(Long.valueOf(99999));
        reviewDashBoardDTO.setReviewChatDTO(chatDTO);
    }

    private void getReviewAccompanyPlayStatistic(String startTime, String endTime, ReviewDashBoardDTO reviewDashBoardDTO)
    {
        ReviewAccompanyPlayDTO accompanyPlayDTO = new ReviewAccompanyPlayDTO();
        accompanyPlayDTO.setVerify(userSkillAdminDOMapper.getReviewUserSkillCount(startTime, endTime));
        accompanyPlayDTO.setAppraise(accompanyAppraiseDOMapper.getReviewAccompanyAppraiseCount(startTime,endTime));
        accompanyPlayDTO.setAppeal(accompanyAfterSaleApplyDOMapper.getReviewAccompanyAfterSaleCount(startTime, endTime));
        reviewDashBoardDTO.setReviewAccompanyPlayDTO(accompanyPlayDTO);
    }

    private void getReviewOtherStatistic(String startTime, String endTime, ReviewDashBoardDTO reviewDashBoardDTO)
    {
        ReviewOtherDTO reviewOtherDTO = new ReviewOtherDTO();
        reviewOtherDTO.setTip(userTipDOMapper.getReviewUserTipCount(startTime, endTime));
        reviewOtherDTO.setWiithdraw(withdrawApplyDOMapper.getReviewWithdrawCount(startTime, endTime));
        reviewDashBoardDTO.setReviewOtherDTO(reviewOtherDTO);
    }

    @Override
    public ReviewDashBoardDTO getReviewDashboardStatistic(String startTime, String endTime) {
        ReviewDashBoardDTO reviewDashBoardDTO = new ReviewDashBoardDTO();
        getReviewUserInfoStatistic(startTime, endTime, reviewDashBoardDTO);
        getReviewSquareStatistic(startTime, endTime, reviewDashBoardDTO);
        getReviewChatStatistic(startTime, endTime, reviewDashBoardDTO);
        getReviewAccompanyPlayStatistic(startTime, endTime, reviewDashBoardDTO);
        getReviewOtherStatistic(startTime, endTime, reviewDashBoardDTO);
        return reviewDashBoardDTO;
    }

    @Override
    public BaseRsp getOKRConfList() {
        int count = okrConfDOMapper.getOKRConfListCount();
        List<OKRConfDO> listConf = okrConfDOMapper.getOKRConfList();
        JSONObject jsonObj = new JSONObject();
        jsonObj.put("count", count);
        jsonObj.put("items", listConf);
        return new BaseRsp(ResultEnum.SUCCESS, jsonObj);
    }

    @Override
    public BaseRsp getOKRDataList(String okrType, String startTime, String endTime, String offset, String limit) {
        List<OKRDataDO> listTotal = okrDataDOMapper.getOKRDataListTotal(okrType, startTime, endTime);
        List<OKRDataDO> listConf = okrDataDOMapper.getOKRDataList(okrType, startTime, endTime, Integer.parseInt(offset), Integer.parseInt(limit));
        JSONObject jsonObj = new JSONObject();
        jsonObj.put("count", listTotal.size());
        jsonObj.put("items", listConf);
        if (okrType.equals("okr1")) {
            JSONObject totalInfo = new JSONObject();
            VoiceRoomDataQueryDTO queryDTO = new VoiceRoomDataQueryDTO();
            queryDTO.setRebateType(3);
            queryDTO.setEndTime(endTime);
            totalInfo.put("room_num", voiceRoomMapper.getVoiceRoomNum(queryDTO));
            totalInfo.put("sum_profit", 0);
            totalInfo.put("avg_profit", 0);
            for (OKRDataDO item : listTotal) {
                JSONObject okrValue = JSONObject.parseObject(item.getOkrValue());
                totalInfo.put("sum_profit", totalInfo.getInteger("sum_profit")+okrValue.getInteger("sum_profit"));
                totalInfo.put("avg_profit", totalInfo.getInteger("avg_profit")+okrValue.getInteger("avg_profit"));
            }
            jsonObj.put("totalInfo", totalInfo);
        } else if (okrType.equals("okr2")) {
            JSONObject totalInfo = new JSONObject();
            VoiceRoomDataQueryDTO queryDTO = new VoiceRoomDataQueryDTO();
            queryDTO.setRebateType(2);
            queryDTO.setEndTime(endTime);
            totalInfo.put("room_num", voiceRoomMapper.getVoiceRoomNum(queryDTO));
            totalInfo.put("sum_profit", 0);
            for (OKRDataDO item : listTotal) {
                JSONObject okrValue = JSONObject.parseObject(item.getOkrValue());
                totalInfo.put("sum_profit", totalInfo.getInteger("sum_profit")+okrValue.getInteger("sum_profit"));
            }
            jsonObj.put("totalInfo", totalInfo);
        } else if (okrType.equals("okr12")) {
            JSONObject totalInfo = new JSONObject();
            totalInfo.put("profit", 0);
            for (OKRDataDO item : listTotal) {
                JSONObject okrValue = JSONObject.parseObject(item.getOkrValue());
                totalInfo.put("profit", totalInfo.getInteger("profit")+okrValue.getInteger("profit"));
            }
            jsonObj.put("totalInfo", totalInfo);
        }
        return new BaseRsp(ResultEnum.SUCCESS, jsonObj);
    }

    @Override
    public BaseRsp getOKRDataLine(String okrType, String startTime, String endTime, String offset, String limit) {
        int count = okrDataDOMapper.getOKRDataLineCount(okrType, startTime, endTime);
        List<OKRDataDO> listConf = okrDataDOMapper.getOKRDataLine(okrType, startTime, endTime, Integer.parseInt(offset), Integer.parseInt(limit));
        JSONObject jsonObj = new JSONObject();
        jsonObj.put("count", count);
        jsonObj.put("items", listConf);
        return new BaseRsp(ResultEnum.SUCCESS, jsonObj);
    }

    @Override
    public BaseRsp getUserRegisterList(String startTime, String endTime, Integer offset, Integer limit) {
        int count = userRegDashboardDOMapper.getUserRegisterCount(startTime, endTime);
        List<UserRegDashboardDO> items = userRegDashboardDOMapper.getUserRegisterList(startTime, endTime, offset, limit);
        JSONObject jsonObj = new JSONObject();
        JSONArray jsonItems = new JSONArray();
        for (UserRegDashboardDO item : items) {
            jsonItems.add(item.getListItem());
        }
        jsonObj.put("count", count);
        jsonObj.put("items", jsonItems);
        return new BaseRsp(ResultEnum.SUCCESS, jsonObj);
    }

    @Override
    public BaseRsp getUserRegisterStatistic(String category, String startTime, String endTime) {
        JSONObject jsonObj = new JSONObject();
        jsonObj.put("title", UserRegDashboardDO.getLegendTitle(category));
        jsonObj.put("legend", UserRegDashboardDO.getLegendList(category));
        jsonObj.put("xAxis", new JSONArray());
        jsonObj.put("series", new JSONArray());

        DateUtil dateUtil = new DateUtil();
        List<UserRegDashboardDO> items = userRegDashboardDOMapper.getUserRegisterStatistic(startTime, endTime);
        for (int idx=0; idx<jsonObj.getJSONObject("legend").getJSONArray("data").size(); ++idx) {
            String legend = jsonObj.getJSONObject("legend").getJSONArray("data").getString(idx);
            JSONObject xAxisItem = new JSONObject();
            xAxisItem.put("data", new JSONArray());
            JSONObject seriesItem = new JSONObject();
            seriesItem.put("name", legend);
            seriesItem.put("type", UserRegDashboardDO.getLegendSeriesType(legend));
            seriesItem.put("yAxisIndex", UserRegDashboardDO.getLegendSeriesyAxisIndex(legend));
            seriesItem.put("areaStyle", JSONObject.parseObject("{normal: {}}"));
            seriesItem.put("data", new JSONArray());
            for (UserRegDashboardDO item : items) {
                xAxisItem.getJSONArray("data").add(dateUtil.dateToStr(item.getDtdate()));
                seriesItem.getJSONArray("data").add(item.getLegendSeriesData(legend));
            }
            if (idx == 0) {
                jsonObj.getJSONArray("xAxis").add(xAxisItem);
            }
            jsonObj.getJSONArray("series").add(seriesItem);
        }
        return new BaseRsp(ResultEnum.SUCCESS, jsonObj);
    }

    @Override
    public BaseRsp getUserLoginList(String startTime, String endTime, Integer offset, Integer limit) {
        int count = userActDashboardDOMapper.getUserLoginCount(startTime, endTime);
        List<UserActDashboardDO> items = userActDashboardDOMapper.getUserLoginList(startTime, endTime, offset, limit);
        JSONObject jsonObj = new JSONObject();
        JSONArray jsonItems = new JSONArray();
        for (UserActDashboardDO item : items) {
            jsonItems.add(item.getListItem());
        }
        jsonObj.put("count", count);
        jsonObj.put("items", jsonItems);
        return new BaseRsp(ResultEnum.SUCCESS, jsonObj);
    }

    @Override
    public BaseRsp getUserLoginStatistic(String category, String startTime, String endTime){
        JSONObject jsonObj = new JSONObject();
        jsonObj.put("title", UserActDashboardDO.getLegendTitle(category));
        jsonObj.put("legend", UserActDashboardDO.getLegendList(category));
        jsonObj.put("xAxis", new JSONArray());
        jsonObj.put("series", new JSONArray());

        DateUtil dateUtil = new DateUtil();
        List<UserActDashboardDO> items = userActDashboardDOMapper.getUserLoginStatistic(startTime, endTime);
        for (int idx=0; idx<jsonObj.getJSONObject("legend").getJSONArray("data").size(); ++idx) {
            String legend = jsonObj.getJSONObject("legend").getJSONArray("data").getString(idx);
            JSONObject xAxisItem = new JSONObject();
            xAxisItem.put("data", new JSONArray());
            JSONObject seriesItem = new JSONObject();
            seriesItem.put("name", legend);
            seriesItem.put("type", UserActDashboardDO.getLegendSeriesType(legend));
            seriesItem.put("yAxisIndex", UserActDashboardDO.getLegendSeriesyAxisIndex(legend));
            seriesItem.put("areaStyle", JSONObject.parseObject("{normal: {}}"));
            seriesItem.put("data", new JSONArray());
            for (UserActDashboardDO item : items) {
                xAxisItem.getJSONArray("data").add(dateUtil.dateToStr(item.getDtdate()));
                seriesItem.getJSONArray("data").add(item.getLegendSeriesData(legend));
            }
            if (idx == 0) {
                jsonObj.getJSONArray("xAxis").add(xAxisItem);
            }
            jsonObj.getJSONArray("series").add(seriesItem);
        }
        return new BaseRsp(ResultEnum.SUCCESS, jsonObj);
    }
}
